﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;
using Reborn_WorldServer;
using System.Timers;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_Mesmerize : Buff
    {
        public Spell_Mesmerize()
        {
            minutesLeft = -2;
            ticksLeft = 9;
            spellTimer.Interval = 3000;
        }

        public override bool tryResist(SpellIcon _spellIcon, int castLevel, int castInt, int castWis, CreatureBase targetCreatureBase)
        {
            int penetration = SkillManager.AdjustForCombat(castLevel, 0, castInt);
            
            if (_spellIcon.Name.IndexOf("Rank 2") > -1 || _spellIcon.SpellType == "Target Area")
                penetration = System.Convert.ToInt32(penetration * 2);

            int resist = targetCreatureBase.getResist(_spellIcon.Skill) * 2;

            int result = Managers.GameHandler.random.Next(1, penetration + resist + 1);
            if (result > penetration)
                return true;

            return false;
        }

        override public void Resume(CreatureBase targetCreatureBase, SpellIcon _spellIcon, int timeRemaining, int castLevel, int castCharacterID, int castInt, int castWis, int optionalValue, string xmlData)
        {
            base.Resume(targetCreatureBase, _spellIcon, timeRemaining, castLevel, castCharacterID, castInt, castWis, optionalValue, xmlData);
            if (TargetCreatureBase is Character)
            {
                Character character = (Character)TargetCreatureBase;
                character.SetBusy(true);
            }

            if (_spellIcon.SpellType == "Target Area" && timeRemaining == -1)
                ticksLeft = 12;

            if (timeRemaining != -1)
                ticksLeft = timeRemaining;

            spellTimer.Start();
        }

        public void Cast(CreatureBase myCreatureBase, int CastLevel, long CreatureGUID)
        {
            List<MapItem> MapItemsWithin12;
            if (base.InitializeCast(myCreatureBase, CastLevel, CreatureGUID, true, out MapItemsWithin12) == false)
                return;

            if (TargetCreatureBase is Character)
            {
                Character character = (Character)TargetCreatureBase;
                character.SetBusy(true);
                character.buffManager.UpdateBuffs();
            }

            spellTimer.Start();
        }
        public override void RemoveSpell(bool updateBuffs, bool sendMessage)
        {
            base.RemoveSpell(updateBuffs, sendMessage);

            if (TargetCreatureBase is Character)
            {
                Character character = (Character)TargetCreatureBase;
                character.SetBusy(false);
            }
        }

        protected override void SpellTimer_Expired(object source, ElapsedEventArgs e)
        {
            LockItem lockitem = TargetCreatureBase.GameHandler.GetLockItem(TargetCreatureBase.MapID, TargetCreatureBase.Instance);

            lock (lockitem.LockObj)
            {
                //This buff has been removed, do nothing (this can occur if the buff gets overwritten exactly when this timer fires)
                if (TargetCreatureBase.buffManager.hasBuff(this.SpellID) == false)
                    return;

                if (ticksLeft > 0)
                {
                    ticksLeft--;
                    spellTimer.Start();
                }
                else
                    RemoveSpell(true, true);
            }
        }
    }
}
